﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OneSystems.Data.Entities
{
    public partial class Debtor : BaseEntity<Debtor>
    {
        #region Public Properties
        public decimal TotalDebtBalance
        {
            get
            {
                decimal returnValue = 0;
                foreach (Account account in Accounts)
                {
                    returnValue += account.CurrentBalance;
                }
                return returnValue;
            }
        }
        public decimal TotalMinimumPayments
        {
            get
            {
                decimal returnValue = 0;
                foreach (Account account in Accounts)
                {
                    returnValue += account.MinPayment;
                }
                return returnValue;
            }
        }
        public PledgeHistory CurrentPledge
        {
            get
            {
                PledgeHistory pledgeHistory = null;
                if (PledgeHistories.Count > 0)
                {
                    pledgeHistory = PledgeHistories.OrderByDescending(c => c.EffectiveDate).FirstOrDefault();
                }
                return pledgeHistory;
            }
        }
        public decimal PledgeAmount
        {
            get
            {
                return CurrentPledge != null ? CurrentPledge.Amount : 0;
            }
            set
            {
                if (PledgeAmount != value)
                {
                    PledgeHistory newPledge = new PledgeHistory();
                    newPledge.Amount = value;
                    newPledge.EffectiveDate = DateTime.Now;
                    newPledge.DebtoryID = DebtorID;
                    newPledge.Save();
                }
            }
        }
        #endregion
    }
}
